// get the client
const Sequelize = require('sequelize')
const SequelizeAuto = require('sequelize-auto')
const path = require('path')

const modelPath = path.join(__dirname, '/model')
module.exports = class Class {
  constructor () {
  }

  async init (config) {
    const tables = [
      'spider_platform',
      'spider_anchor',
      'spider_anchor_data',
      'spider_type']
    const auto = new SequelizeAuto(config.database, config.user,
      config.password, {
        host: config.host,
        dialect: 'mysql',
        // directory: false, // prevents the program from writing to disk
        directory: modelPath,
        additional: {
          timestamps: false,
        },
        port: config.port,
        tables,
      })

    this.sequelize = new Sequelize(config.database, config.user,
      config.password,
      {
        dialect: 'mysql',
        host: config.host,
        port: config.port,
        logging: false,
        timezone: '+08:00',
        pool: {
          max: 60,
          min: 10,
        },
      })

    this.model = {}

    await new Promise((resolve, reject) => {
      auto.run(err => {
        if (err)
          return reject(err)
        tables.forEach(table => {
          console.log('import', table)
          this.model[table] = this.sequelize.import(
            modelPath + '/' + table + '.js')
        })
        resolve()
      })
    })
    await this.sequelize.sync()
  }

  /** @namespace this.model.spider_anchor */
  /** @namespace this.model.spider_anchor_data */

  /** @namespace this.model.spider_platform */
  /** @namespace this.model.spider_type */

}